//
//  LDRetweetView.swift
//  Weibo201655
//
//  Created by pro on 16/5/11.
//  Copyright © 2016年 LeeDan. All rights reserved.
//

import UIKit
import SnapKit

class LDRetweetView: UIView {
    
    //MARK:----------------------- 属性 ----------------------------------//
    var retweetStatus: LDStatus? {
        didSet {
            let name = retweetStatus?.user?.screen_name ?? "无名"
            let content = retweetStatus?.text ?? "无微博内容"
            
            retweetContentLabel.text = "@\(name): \(content)"
            
            // 判断是否有图片需要显示
            retweetViewBottomConstraint?.uninstall()//去除约束
            if let _ = retweetStatus?.pictureUrls {
                retweetPictureView.hidden = false
                retweetPictureView.status = retweetStatus
                //更新底部约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(retweetPictureView).offset(LDStatusCellMargin).constraint
                })
            }else {
                retweetPictureView.hidden = true
                //更新底部约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(retweetContentLabel).offset(LDStatusCellMargin).constraint
                })
            }
        }
    }
    
    //底部约束
    var retweetViewBottomConstraint: Constraint?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        addSubview(bgButton)
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        
        //布局
        bgButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(LDStatusCellMargin)
            make.trailing.equalTo(self).offset(-LDStatusCellMargin)
        }
        
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(LDStatusCellMargin)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(LDStatusCellMargin)
            
            make.size.equalTo(CGSizeMake(200, 200))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewBottomConstraint = make.bottom.equalTo(retweetPictureView).offset(LDStatusCellMargin).constraint
        }
    }
    
    //MARK:----------------------- 懒加载 ----------------------------------//
    private lazy var bgButton: UIButton = {
        let btn = UIButton()
        
        //背景图片
        btn.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        
        return btn
    }()
    
    private lazy var retweetContentLabel: UILabel = {
        let label = UILabel()

        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * LDStatusCellMargin
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.darkGrayColor()
        
        return label
    }()
    
    private lazy var retweetPictureView: LDCellCollectionView = LDCellCollectionView()
}
